<?php
include_once('./common.php');
$cb = empty($_GET['cb'])? "cbs" : $_GET['cb'];
$cid = empty($_GET['cid'])? 0 : $_GET['cid'];
//登录判断
if(empty($_SGLOBAL['supe_uid'])) {
	showmessage('to_login', 'do.php?ac='.$_SCONFIG['login_action']);
}
//站点关闭
checkclose();

if($cb == "cbs"){//征稿活动管理
	include_once('./source/function_common.php');
	$page = empty($_GET['page'])? 1 : intval($_GET['page']);
	if($page < 1){
		$page=1;
	}
	$perpage = 10; //每页要显示的条数
	$start = ($page-1)*$perpage;
	ckstart($start, $perpage);
	$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('contribute')." WHERE uid =".$_SGLOBAL['supe_uid']),0);
	$multi = multi($count, $perpage, $page, "contribute.php");

	$query = $_SGLOBAL['db']->query("SELECT c.*,t.* FROM ".tname('contribute')." AS c LEFT JOIN ".tname('contribute_type')." AS t ON c.typeid = t.tid WHERE c.uid =".$_SGLOBAL['supe_uid']." ORDER BY c.ontime DESC LIMIT ".$start.",".$perpage);
	while($value = $_SGLOBAL['db']->fetch_array($query)){
		$cbs[] = $value;
	}
	include template("contribute");
}elseif($cb == "cbl"){//查看征稿活动
	if(empty($cid)){
		showmessage('contribute_no_cid', 'contribute.php');
	}
	$query = $_SGLOBAL['db']->query("SELECT c.*,t.type,f.picid,f.filename,f.fileext FROM ".tname('contribute')." AS c LEFT JOIN ".tname('contribute_type')." AS t ON c.typeid = t.tid LEFT JOIN ".tname('files')." AS f ON c.file = f.filepath WHERE c.cid =".$cid);
	$cont_look = $_SGLOBAL['db']->fetch_array($query);
	include template("contribute_look");
}elseif($cb == "add"){//去添加征稿活动
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('contribute_type')." WHERE isopen != 1");
	while($type = $_SGLOBAL['db']->fetch_array($query)){
		$cont_type[] = $type;
	}
	include template("contribute_add");
}elseif($cb == "addsave"){//添加征稿活动
	$adddata['title'] = $_POST['title'] ? trim($_POST['title']) : "";
	$adddata['content'] = $_POST['content'] ? trim($_POST['content']) : "";
	$oldfile = $_POST['file'] ? trim($_POST['file']) : "";
	$newfile = explode("/attachment/",$oldfile);
	$adddata['file'] = $newfile[1];
	$adddata['uid'] = $_SGLOBAL['supe_uid'];
	$adddata['ontime'] = time();
	$adddata['typeid'] = intval($_POST['type']);
	$adddata['show'] = intval($_POST['show']);
	$insert = inserttable('contribute',$adddata,1);
	if($insert){
		showmessage('contribute_success', 'contribute.php');
	}else{
		showmessage('contribute_fail', 'contribute.php?cb=add');
	}
}
?>